#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModbusDataUnit>
#include <QGridLayout>
#include <QTimer>
#include <QMediaPlayer>
#include <QAudioOutput>

#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QStyleFactory>
#include <QActionGroup>
#include <QSqlTableModel>

#include "formsub.h"
#include "kb9000.h"
#include "mydelegate.h"

QT_BEGIN_NAMESPACE

class QModbusClient;

namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QMediaPlayer* player;
    QAudioOutput* audioOutput;

private:
    void readData(int index);
    void drawForm(); // 在主页面中画探测器显示面板
    bool dbConnection();

private slots:
    void onModbusStateChanged(int state);
    void on_actionOption_triggered();
    void on_actionConnect_triggered();
    void onReadReady();
    void readDataTimer();
    void playWarningSound();    // 播放报警声音
    void on_actionDisconnect_triggered();
    void on_actionQuit_triggered();
    void on_actionReadData_triggered();
    void on_actionAbout_triggered();
    void on_actionAlarm_triggered();
    void on_actionHistory_triggered();
    void on_actionactionAlarmCancel_triggered();
    void alarmDataChanged();

private:
    Ui::MainWindow *ui;
    QModbusClient *modbusDevice = nullptr;
    QString set_host = "127.0.0.1";
    FormSub* w[MAX_CARD_COUNT];
    QHash<int, FormSub*> ws;   //modbus地址跟DragFrame关联，通过modbus地址访问DragFrame
    int set_port, set_server, set_register, set_poll_time, set_number_of_retries;
    int set_max_col,    // 主界面探测器显示最大行数
        set_max_row,    // 主界面探测器每行显示子面板个数
        set_volume,
        useb_list_index = 0,    // 已启用探测器索引列表序号
        register_index = 0; // 探测器的序号，从 1 开始编号。
    int card_count;     // 一次性读取多少
    QTimer* readTimer;
    QSqlDatabase db;
    QSqlTableModel * tablemodel;
    QList<int> used_list;   // 已启用探测器索引列表
    bool alarmSound = true;
};
#endif // MAINWINDOW_H
